﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using TI_4_Limonade_Fabriek.Database;
using TI_4_Limonade_Fabriek.Model;
using TI_4_Limonade_Fabriek.Views;
using TI_4_Limonade_Fabriek.Logging;
using TI_4_Limonade_Fabriek.OPC;


namespace TI_4_Limonade_Fabriek
{
    public partial class LoginScreen : Form
    {
        
        UserDBController udbc;
        ErrorLogging er = new ErrorLogging();
        BatchController bc;
        Thread threadUitvoeren;
        
        public LoginScreen()
        {
            udbc = new UserDBController();
            InitializeComponent(); //hiermee wordt het visuele style van het programma gecreerd.
            bc = new BatchController();
          
            threadUitvoeren = new Thread(new ThreadStart(bc.Sequiencieel));
            threadUitvoeren.Start();
        }

        private void LoginScreen_Load(object sender, EventArgs e)
        {
            
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbUsername.Text != "" && tbPassword.Text != "")
            {
                User user = new User();
                user.Username = tbUsername.Text;
                user.Password = user.HashPassword(tbPassword.Text);

                user = udbc.CheckLogin(user);
                if (user != null)
                {
                    switch (user.UserRole)
                    {
                        case 0:
                            this.Hide();
                            OperatorOverview ooScreen = new OperatorOverview();
                            ooScreen.ShowDialog();

                            if (ooScreen.IsDisposed)
                            {
                                ResetLoginScreen();
                            }

                            break;

                        case 1:
                            this.Hide();
                            BatchOverviewScreen boScreen = new BatchOverviewScreen();
                            boScreen.ShowDialog();

                            if (boScreen.IsDisposed)
                            {
                                ResetLoginScreen();
                            }
                            break;

                        case 2:
                            this.Hide();
                            ConfigScreen csScreen = new ConfigScreen();
                            csScreen.ShowDialog();

                            if (csScreen.IsDisposed)
                            {
                                ResetLoginScreen();
                            }
                            break;
                    }
                }
                else
                {
                    MessageBox.Show("Onjuiste inloggegevens", "Ongeldig", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    er.LogErrorMsg("Onjuiste inloggegevens : Gebruiker: " + tbUsername.Text);
                }
            }
            else
            {
                MessageBox.Show("Voer aub gegevens in.");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
           
        }

        private void ResetLoginScreen()
        {
            this.Show();
            tbUsername.Text = "";
            tbPassword.Text = "";
        }

        private void tbUsername_TextChanged(object sender, EventArgs e)
        {

        }

    

        private void LoginScreen_FormClosed(object sender, FormClosedEventArgs e)
        {
            bc.stoppen = true;
            threadUitvoeren.Abort();
        } 
    }
}
